package cc.fozone.note.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.function.BiConsumer;

import org.springframework.stereotype.Component;

@Component
public class AppConfig implements IConfiguration<String,String> {
	private static String LOCATION = "/app.properties";
	private Properties properties;
	private Map<String,String> map;
	public AppConfig() {
		this.initConfig();
		this.initMap();
	}
	
	private void initConfig() {
		InputStream inputStream = null;
		properties = new Properties();
		inputStream = AppConfig.class.getResourceAsStream(LOCATION);
		try {
			if(inputStream == null) return;
			properties.load(inputStream);
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void initMap() {
		map = new HashMap<String,String>();
		properties.forEach(new BiConsumer<Object, Object>() {

			@Override
			public void accept(Object key, Object value) {
				// TODO Auto-generated method stub
				map.put((String) key, (String)value);
			}
		});
	}

	@Override
	public String get(String key) {
		// TODO Auto-generated method stub

		return map.get(key);
	}
	
	@Override
	public Map<String, String> getProperties() {
		// TODO Auto-generated method stub
		return map;
	}

	@Override
	public String get(String key, String defaultValue) {
		// TODO Auto-generated method stub
		System.out.println(key+ "=" + map.get(key));
		return map.getOrDefault(key, defaultValue);
	}
	
}
